<?php
/**
 * 商品类
 */
namespace App\Controller\Goods;


use Common\Model;
use Common\Controller\App\SignController;

class IndexController extends SignController{

    /**
     * 商品关键词搜索
     */
    public function keywords_search(){
        $stock_id = I('post.stock_id');   //仓库id
        $cat_id   = I('post.cat_id');    //分类id
        $xinpin  = I('post.xinpin');    //新品排序 desc   asc
        $sale     = I('post.sale');    //销量排序  desc   asc
        $price     = I('post.price');    //价格排序 desc   asc
        $keyword = I('post.keyword');
        $page = I('post.page')?I('post.page'):1;
        $limit = I('post.limit')?I('post.limit'):20;
        $num = ($page-1)*$limit;
        $model = new Model\ProductSkuModel();
        $res = $model->keyword_search($keyword,$num,$limit,$stock_id,$cat_id,$xinpin,$sale,$price);
        $this->ajaxReturn(['code'=>200,'res'=>$res]);
    }

    /**
     * 商品列表
     */
    public function lists(){
        $page = I('post.page')?I('post.page'):1;  //页码
        $limit  = I(('post.limit'))?I(('post.limit')):20; //条数
        $catId  = I('post.catId')?I('post.catId'):0;    //分类id
        $num = ($page-1)*$limit;
        $model = new Model\ProductSkuModel();
        $list = $model->lists($catId,$num,$limit);
        if($catId){
            $catmodel = new Model\ProductCatModel();
            $catlist = $catmodel->cat_list($catId);
        }
        if(empty($catlist)){
            $catlist = [];
        }
        $this->ajaxReturn(['code'=>200,'list'=>$list,'catlist'=>$catlist,'catId'=>$catId,'page'=>$page]);
    }

    /**
     * 中国馆商品列表
     */
    public function china_goods_list(){
        $page = I('post.page')?I('post.page'):1;  //页码
        $limit  = I(('post.limit'))?I(('post.limit')):20; //条数
        $num = ($page-1)*$limit;
        $model = new Model\ProductSkuModel();
        $list = $model->guojia_goods_list('',$num,$limit);
        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }

    /**
     * 国家馆商品列表
     */
    public function guojia_goods_list(){
        $guojia = I('post.guojia');
        $id = I('post.id');
        $model = new Model\BrandCatModel();
        $info = $model->where("id=$id")->field("banner,title,describes")->find();
        $page = I('post.page')?I('post.page'):1;  //页码
        $limit  = I(('post.limit'))?I(('post.limit')):20; //条数
        $num = ($page-1)*$limit;
        $model = new Model\ProductSkuModel();
        $list = $model->guojia_goods_list($guojia,$num,$limit);
        $this->ajaxReturn(['code'=>200,'list'=>$list,'info'=>$info]);
    }

    /**
     * 品牌馆商品列表
     */
    public function brand_goods_list(){
        $id = I('post.id');
        $model = new Model\BrandModel();
        $info = $model->where("id=$id")->field("logo_large,title,describes")->find();
        $page = I('post.page')?I('post.page'):1;  //页码
        $limit  = I(('post.limit'))?I(('post.limit')):20; //条数
        $num = ($page-1)*$limit;
        $model = new Model\ProductSkuModel();
        $list = $model->brand_goods_list($id,$num,$limit);
        $this->ajaxReturn(['code'=>200,'list'=>$list,'info'=>$info]);
    }

    /**
     * 商品详情
     */
    public function goods_view(){
        $skuid = I('post.skuid');
        if(empty($skuid)){
            $this->ajaxReturn(['code'=>300,'msg'=>'商品编号错误']);
        }
        $model = new Model\ProductSkuModel();
        $info = $model->getRow($skuid);
        $this->ajaxReturn(['code'=>200,'info'=>$info]);
    }

    /**
     * 秒杀商品详情
     */
    public function ms_goods_view(){
        $id = I('post.skuid');
        if(empty($id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'商品编号错误']);
        }
        $model = new Model\ProductSpikeModel();
        $info = $model->getOne($id);
        $this->ajaxReturn(['code'=>200,'info'=>$info]);
    }

    /**
     * 点赞商品
     */
    public function zan(){
        $skuid = I('post.skuid');
        if(empty($skuid)){
            $this->ajaxReturn(['code'=>300,'msg'=>'商品编号错误']);
        }
        $model = new Model\ProductSkuModel();
        $sale = $model->zan($skuid);
        $this->ajaxReturn(['code'=>200,'thing_num'=>$sale]);
    }
    /**
     * 取消点赞商品
     */
    public function clear_zan(){
        $skuid = I('post.skuid');
        if(empty($skuid)){
            $this->ajaxReturn(['code'=>300,'msg'=>'商品编号错误']);
        }
        $model = new Model\ProductSkuModel();
        $sale = $model->clear_zan($skuid);
        $this->ajaxReturn(['code'=>200,'thing_num'=>$sale]);
    }
}